<template>
  <div v-loading="loading">
    <div class="comment-head">
      评论
      <span v-if="commentTotal">({{ commentTotal > 0 ? commentTotal : '' }}条)</span>
    </div>

    <comment-form @send="(v, c) => confirmComment(v, c)"></comment-form>
    <div class="comment">
      <div v-for="(item, index) in commentList" :key="index" class="comment-item">
        <div class="head">
          <div class="left">
            <div class="head-img">
              <img :src="item.headImg" alt="" />
            </div>
            <div class="head-info">
              <span>{{ item.nickname }}</span>
              <gender v-if="item.gender" :gender="item.gender"></gender>
              <span v-if="item.role == 2" class="blogger">博主</span>
              <span v-if="item.territory" class="ip">IP:{{ item.territory }}</span>
            </div>
          </div>
          <!-- <div class="right">回复</div> -->
        </div>
        <!-- 内容 -->
        <div class="comment-text">
          <html-text :content="item.content"></html-text>
        </div>
        <!--时间,回复 -->
        <div class="bottom">
          <div class="left">
            <span class="date">{{ $sessionTime(item.createDate) }}</span>
          </div>
          <div class="right">
            <div v-if="item.state == 1" @click="openReply(item)" class="btn">回复</div>
          </div>
        </div>
        <div v-if="publishComment.parentId == item.commentId" style="padding-left: 40px">
          <comment-form @send="(v, c) => confirmReply(v, c, item, item)" :placeholder="placeholder"></comment-form>
        </div>

        <!-- 回复列表 -->
        <div v-if="item.childrens.list[0]" class="reply">
          <template v-for="(value, key) in item.childrens.list">
            <div :key="key" class="comment-item">
              <!-- 头部 -->
              <div class="head">
                <div class="left">
                  <div class="head-img">
                    <img :src="value.headImg" alt="" />
                  </div>
                  <div class="head-info">
                    <span>{{ value.nickname }}</span>
                    <span style="color: #999">回复</span>
                    <span>{{ value.parentNickname }}</span>
                    <gender v-if="value.gender" :gender="value.gender"></gender>
                    <span v-if="value.role == 1" class="blogger">博主</span>
                    <span v-if="value.territory" class="ip">IP:{{ value.territory }}</span>
                  </div>
                </div>
              </div>
              <!-- 内容 -->
              <div class="comment-text"><html-text :content="value.content"></html-text></div>
              <!--时间,回复 -->
              <div class="bottom">
                <div class="left">
                  <span class="date">{{ $sessionTime(value.createDate) }}</span>
                </div>
                <div class="right">
                  <div v-if="value.state == 2" @click="openReply(value)" class="btn">回复</div>
                </div>
              </div>
            </div>
            <div :key="key" v-if="publishComment.parentId == value.commentId" style="padding-left: 40px">
              <comment-form @send="(v, c) => confirmReply(v, c, value, item)" :placeholder="placeholder"></comment-form>
            </div>
          </template>
          <div v-if="item.childrens.list.length < item.childrens.total" class="more more-reply">
            <span v-if="item.loading">
              <i class="el-icon-loading"></i>
              加载中...
            </span>
            <span @click="getChildrenCommentList(item)" v-else>
              剩余{{ item.childrens.total - item.childrens.list.length }}条
              <i class="el-icon-arrow-down"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
    <div v-if="commentList[0]" class="more">
      <span @click="loadingMore" v-if="!isLoadingMore && commentTotal - commentList.length > 0">
        剩余{{ commentTotal - commentList.length }}条
        <i class="el-icon-arrow-down"></i>
      </span>
      <span v-else-if="isLoadingMore && commentTotal - commentList.length > 0">
        <i class="el-icon-loading"></i>
        加载中...
      </span>
      <span v-else>没有更多啦~</span>
    </div>
  </div>
</template>

<script>
import gender from '../gender/gender';
import commentForm from '../comment-form/comment-form';
import htmlText from '@/components/html-text/html-text';

export default {
  name: 'WorkspaceJsonFileComment',
  components: {
    commentForm,
    htmlText,
    gender,
  },
  props: {
    targetId: {
      default: '',
    },
  },
  data() {
    return {
      commentList: [],
      commentTotal: 0,
      loading: true,
      isLoadingMore: false,
      postData: {
        type: 1,
        targetId: '',
        pageSize: 5,
        pageIndex: 1,
        sort: '2',
      },
      // 发布 / 回复
      commentLoading: false,
      publishComment: {
        targetId: '',
        parentId: '',
        type: '1',
        content: '',
      },
      newComment: [],
      placeholder: '',
    };
  },
  watch: {
    targetId: {
      immediate: true,
      handler() {
        this.publishComment.targetId = this.targetId;
        this.postData.targetId = this.targetId;
        if (this.targetId) this.byIdGetCommentList();
      },
    },
  },
  computed: {
    userInfo() {
      return this.$store.state.userInfo;
    },
  },
  mounted() {},
  methods: {
    // 获取评论列表
    byIdGetCommentList() {
      this.$http('byIdGetCommentList', this.postData).then(res => {
        this.commentTotal = res.data.total;
        let list = res.data.lists.map(i => {
          i.loading = false;
          i.childrens.pageSize = 5;
          i.childrens.pageIndex = 1;
          return i;
        });
        this.commentList = [...this.commentList, ...list];
        this.loading = false;
        this.isLoadingMore = false;
      });
    },
    // 分页
    loadingMore() {
      this.isLoadingMore = true;
      if (this.commentList.length < this.commentTotal) {
        this.isLoadingMore = true;
        this.postData.pageIndex += 1;
        this.byIdGetCommentList();
      }
    },
    // 获取子评论
    getChildrenCommentList(item) {
      let postData = {
        parentId: item.commentId,
        pageSize: item.childrens.pageSize,
        pageIndex: item.childrens.pageIndex,
        sort: '2',
      };
      item.loading = true;
      this.$http('getChildrenCommentList', postData).then(res => {
        if (item.childrens.pageIndex == 1) {
          item.childrens.list = res.data.lists;
        } else {
          item.childrens.list = [...item.childrens.list, ...res.data.lists];
        }
        item.childrens.pageIndex += 1;
        item.loading = false;
      });
    },
    // 确认发送评论
    confirmComment(data, clearData) {
      this.commentLoading = true;
      this.publishComment.parentId = '';
      this.publishComment.content = data.content;
      this.$http('publishComment', this.publishComment).then(res => {
        res.data.childrens = {
          list: [],
          total: 0,
          pageSize: 5,
          pageIndex: 1,
        };
        this.commentList.unshift(res.data);
        this.commentLoading = false;
        this.commentTotal += 1;
        clearData();
      });
    },
    // 打开回复
    openReply(item) {
      this.placeholder = `回复 : ${item.nickname}`;
      this.publishComment.parentId = item.commentId;
    },
    // 确认回复
    confirmReply(data, clearData, item, parent) {
      item.loading = true;
      this.publishComment.content = data.content;
      this.$http('publishComment', this.publishComment).then(res => {
        parent.childrens.list.unshift(res.data);
        item.loading = false;
        parent.childrens.total += 1;
        this.publishComment.parentId = '';
        clearData();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.comment-head {
  padding: 10px 0 20px;
  text-align: center;
}
.comment {
  margin-top: 20px;
  padding: 10px 10px 10px 0;
  // 评论
  .more {
    display: inline-block;
    font-size: 12px;
    color: #999;
    margin-top: 5px;
    // border: 1px solid #cccccc;
    border-radius: 3px;
    padding: 2px;
    cursor: pointer;
  }
}

.comment-form {
  .head {
    width: 35px;
    height: 35px;
    background: #f5f5f5;
    line-height: 35px;
    text-align: center;
    color: #999;
    border-radius: 5px;
    margin-right: 10px;
  }
  .el-form-item {
    margin-bottom: 10px;
    margin-right: 10px;
  }

  .row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    justify-content: center;
    .el-form-item {
      flex: 1;
      min-width: 250px;
      margin-bottom: 0;
    }
    .form-item-gender {
      min-width: 100px;
      padding-right: 10px;
      flex: none;
      //   width: 140px;
    }
  }
}

// 评论每一项
.comment-item {
  margin-bottom: 10px;
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    .left {
      display: flex;
      align-items: center;
      .head-img {
        width: 30px;
        height: 30px;
        background: #ccc;
        margin-right: 10px;
        border-radius: 5px;
        overflow: hidden;
      }
      .head-info {
        color: #607199;
        display: flex;
        align-items: center;
        .ip {
          color: #999;
          margin: 0 5px;
        }
        .blogger {
          background: #8cc9a4;
          color: #fff;
          padding: 1px 5px;
          border-radius: 4px;
          font-size: 12px;
          margin-left: 5px;
        }
        span {
          margin-right: 5px;
        }
      }
    }
    // .right {
    //   color: #607199;
    // }
  }

  .comment-text {
    margin-left: 40px;
    margin-top: 10px;
    font-size: 15px;
    background: #f5f7f9;
    padding: 10px;
    border-radius: 4px;
    color: #666;
  }

  .bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-left: 40px;
    padding: 5px;
    font-size: 14px;
    color: #c5c5c5;
    .btn {
      color: #607199;
    }
  }

  .reply {
    margin-left: 40px;
    padding: 10px 10px 10px 0;
    margin-top: 10px;
    .comment-item {
      margin-bottom: 18px;
    }
    .more-reply {
      margin-left: 40px;
    }
  }
}

.more {
  text-align: center;
  color: #999;
  user-select: none;
  font-size: 14px;
}
</style>
